package com.shujia.flink

import org.apache.flink.streaming.api.TimeCharacteristic
import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
import org.apache.flink.api.scala._
import org.apache.flink.streaming.api.functions.AssignerWithPeriodicWatermarks
import org.apache.flink.streaming.api.watermark.Watermark
import org.apache.flink.streaming.api.windowing.time.Time

object Demo4Watermark {
  def main(args: Array[String]): Unit = {
    val env = StreamExecutionEnvironment.getExecutionEnvironment

    //并行度
    env.setParallelism(1)

    //设置实时处理程序事件类型
    env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime)


    //通过socket创建dataStream
    val ds = env.socketTextStream("node1", 7777)


    /**
      * 1,1564108046000
      * 1,1564108051000
      * 1,1564108054000
      * 2,1564108046000
      *
      *
      */

    val eventDS = ds.map(line => {
      val split = line.split(",")
      (split(0), split(1).toLong)
    })

    val wdWater = eventDS.assignTimestampsAndWatermarks(new AssignerWithPeriodicWatermarks[(String, Long)] {

      //最大支持延迟时间
      val maxOutOfOrderness: Long = 5000L // 3.5 seconds

      var currentMaxTimestamp: Long = _

      var a: Watermark = _

      //获取Watermark  当Watermark大于窗口结束事件触发窗口计算
      override def getCurrentWatermark: Watermark = {
        a = new Watermark(currentMaxTimestamp - maxOutOfOrderness)
        a

      }

      //获取EventTIme 列
      override def extractTimestamp(element: (String, Long), previousElementTimestamp: Long): Long = {

        println(element._1 + "_" + element._2 + "_" + currentMaxTimestamp + "_" + a)

        currentMaxTimestamp = Math.max(element._2, currentMaxTimestamp)
        element._2
      }

    })


    wdWater
      .map(t => (t._1, t._2.toString))
      .keyBy(0)
      .timeWindow(Time.seconds(5))
      .reduce((x, y) => (x._1, x._2 + "-" + y._2))
      .print()

    env.execute()


  }
}
